package com.fr.ticket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author 付荣
 * @version 创建时间：2019年4月4日 下午2:18:40
 */
public class ctrip {

	private static final String uri = "https://sec-m.ctrip.com/restapi/soa2/12530/json/ticketSpotSearch?_fxpcqlniredt=09031075110214485266";
	static RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(5000) // 从连接池中获取连接的超时时间
			// 与服务器连接超时时间：httpclient会创建一个异步线程用以创建socket连接，此处设置该socket的连接超时时间
			.setConnectTimeout(5000).setSocketTimeout(5000) // socket读数据超时时间：从服务器获取响应数据的超时时间
			// .setProxy(host)
			.build();
	static CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).build();

	public static void main(String[] args) {
		SearchListParam param = new SearchListParam();
		param.setKeyword("北京");
		param.setPidx(1);
		
		try {
			HttpPost httpPost = new HttpPost(uri);
			StringEntity stringEntity = new StringEntity((param.toString()), "utf-8");
			httpPost.setEntity(stringEntity);
			httpPost.setHeader("Content-Type", "application/json");
            //利用httpClient的execute()方法发送请求并且获取返回参数
            HttpResponse response = httpClient.execute(httpPost);
            String httpEntityContent = getHttpEntityContent(response);
            if (StringUtils.isNotBlank(httpEntityContent)) {
            	JSONObject object = JSON.parseObject(httpEntityContent); 
	            System.out.println(object);
	            JSONObject data = (JSONObject)object.get("data");
	            System.out.println(data);
            	List<JSONObject> list = (List<JSONObject>) data.get("viewspots");
            	ESUtils.writeIndex("product", "product", list);
            }
		} catch (Exception e) {
			System.err.println(e);
		}
	}

	private static String getHttpEntityContent(HttpResponse response) throws IOException, UnsupportedEncodingException {
		// 通过HttpResponse 的getEntity()方法获取返回信息
		HttpEntity entity = response.getEntity();
		if (entity != null) {
			InputStream is = entity.getContent();
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line = br.readLine();
			StringBuilder sb = new StringBuilder();
			while (line != null) {
				sb.append(line + "\n");
				line = br.readLine();
			}
			br.close();
			is.close();
			return sb.toString();
		}
		return "";
	}


}
